﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActiveDs;
using shfa.snomtastic.data.security;
using shfa.snomtastic.data.business;
using System.DirectoryServices;



namespace shfa.snomtastic.data
{
    public static class common
    {
       

        public enum ctlViewMode { List, Add, Edit };

        public static int TryToParse(string value)
        {
            int n;
            Int32.TryParse(value, out n);
            return n;
        }

        public static DateTime GetDateFromLargeInteger(object largeInteger)
        {

            byte[] valBytes = new byte[8];

            IADsLargeInteger longInt = (IADsLargeInteger)largeInteger;
            Int32 low = longInt.LowPart;
            Int32 high = longInt.HighPart;

            BitConverter.GetBytes(low).CopyTo(valBytes, 0);
            BitConverter.GetBytes(high).CopyTo(valBytes, 4);


            return DateTime.FromFileTimeUtc(BitConverter.ToInt64(valBytes, 0));

        }


        public static String LongDateTime(DateTime d)
        {
            if (d == null || d == DateTime.MinValue)
            {
                return string.Empty;
            }
            d = d.ToLocalTime();

            return d.ToLongDateString() + " - " + d.ToLongTimeString();

        }


        public static string GetFormattedValue(ConfigurationSettingInfo si, UserInfo ui, DirectoryEntry adUser, string macAddress, string ipAddress, bool encrypted, bool forPhone)
        {
            AppConfig ac = new AppConfig();

            string MatchRegEx = @"((<\$[\a-z0-9.-]+?>))\G*";

            string val = @si.Value;
            RijndaelEnhanced re = new RijndaelEnhanced();
            if (val != string.Empty & encrypted)
                if (forPhone)
                    val = re.Decrypt(val);
                else
                    return "[Encrypted]";


            MatchCollection mc = Regex.Matches(val, MatchRegEx);
            if (mc.Count > 0)
            {
                for (int i = 0; i < mc.Count; i++)
                {
                    if (mc[i].Value.Length > 0)
                    {
                        string propertyName = mc[i].Value.Substring(2, mc[i].Value.Length - 3);
                        switch (propertyName)
                        {
                            case "password":

                                if (ui.Password != string.Empty)
                                {
                                    val = re.Decrypt(ui.Password);
                                }
                                else val = "";
                                break;
                            case "macaddress":
                                val = macAddress;
                                break;
                            case "ipaddress":
                                val = ipAddress;
                                break;
                            case "siteroot":
                                val = ac.Items["siteRoot"];
                                break;
                            default:
                                if (adUser != null)
                                {
                                    if (adUser.Properties[propertyName] != null)
                                    {
                                        try
                                        {
                                            val = val.Replace(mc[i].Value, adUser.Properties[propertyName].Value.ToString());
                                        }
                                        catch { val = ""; }
                                    }
                                    else
                                    {
                                        val = val.Replace(mc[i].Value, "");
                                    }
                                }
                                break;
                        }
                    }
                }
            }

            if (val != string.Empty)
            {
                val = Regex.Replace(val, @si.Pattern, @si.Replacement);
            }

            return val;
        }

        public static string DblClickScript
        {
            get
            {
                string script = "";
                script += "function OnRowDblClick(sender, eventArgs)   ";
                script += "{";
                script += "document.selection.empty();";
                script += "sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());";
                script += "}     ";

                return script;
            }
        }

    }
}
